//package org.example
//
//import org.apache.spark.sql.SparkSession
//import org.apache.spark.sql.types.{DataTypes, IntegerType, StringType, StructField, StructType}
//
//object fan09{
//  def main(args: Array[String]): Unit = {
//    val spark = SparkSession
//      .builder()
//      .master("local[*]")
//      .appName("spark")
//      .getOrCreate()
//    val sc = spark.sparkContext
//
//    // val scoreFrame = spark.read.csv("src/main/resources/score.txt")
//    //scoreFrame.printSchema()
//    // scoreFrame.show()
//
//    val schemaScore = StructType(Seq(
//      StructField("id", IntegerType, nullable = false),
//      StructField("name", StringType, nullable = true),
//      StructField("course", StringType, nullable = true),
//      StructField("score", IntegerType, nullable = true)
//    ))
////    val scoreFrame = spark.read.schema(schemaScore).csv("src/main/resources/score.txt")
////    val scoreNewName = scoreFrame.toDF("stu_id", "stu_name", "stu_course", "stu_score")
////    // 输出数据模式（列名和类型）
////    scoreFrame.printSchema()
////    // 查看数据
////    scoreFrame.show()
////    scoreNewName.show()
//
//    // json格式数据读取与SQL操作
//    val schema = StructType(Array(
//      StructField("id", IntegerType, nullable = false), // 学号（非空）
//      StructField("name", StringType, nullable = true), // 姓名
//      //StructField("course", StringType, nullable = true), // 科目
//      StructField("age", IntegerType, nullable = true), //年龄
//      StructField("school", DataTypes.createStructType(Array(StructField("schoolName", StringType), StructField("time", StringType)))),
//      StructField("score", DataTypes.createArrayType(IntegerType), nullable = true)//成绩
//
//    ))
//
//    val data = spark.read.schema(schema).json("src/main/resources/json.txt")
//    data.printSchema()
//    data.show()
//
//    // 注册DataFrame为临时视图
//    data.createOrReplaceTempView("students")
//
//    val averageAgeQuery = spark.sql(
//      """
//        |SELECT AVG(age) AS average_age
//        |FROM students
//        |WHERE school.schoolName = '清职院'
//         """.stripMargin)
//
//    // 显示查询结果
//    averageAgeQuery.show()
//    sc.stop()
//
//
//}
//
//}
